// Copyright 2014 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * Directive for the TextInput interaction.
 *
 * IMPORTANT NOTE: The naming convention for customization args that are passed
 * into the directive is: the name of the parameter, followed by 'With',
 * followed by the name of the arg.
 */


/**
 * Function listen() is used to capture the input microphone.
 *
 */

function listen(){

x = document.getElementById('test');
var recognition = new webkitSpeechRecognition();
// Set the continious recognizing that enable the user to make long pauses and dictate large texts.
recognition.continuous = false;

//can display them in the text box immediately after receiving them.
recognition.interimResults = true;

//possible lenguages
recognition.lang = "en";
recognition.lang = "es";

//To start recognizing.
recognition.start();
recognition.stop();

//Now it is needed to initialize the recognition results handler.
recognition.onresult = function (event) {
    for (var i = event.resultIndex; i < event.results.length; ++i) {
        if (event.results[i].isFinal) {
//The input introduced by the user is inserted into the <schema-based-editor> element
	     x.innerHTML=event.results[i][0].transcript;
	     ans=event.results[i][0].transcript;
	    	    
        }
    }
};
}

/** Our problem  is that after inserting the answer in the <schema-based-editor> we do not how to submit that anwser to the server in order to check if it is correct or not. */


ans="none"


oppia.directive('oppiaInteractiveTextInput', [
  'oppiaHtmlEscaper', function(oppiaHtmlEscaper) {
    return {
      restrict: 'E',
      scope: {},
      templateUrl: 'interaction/TextInput',
      controller: ['$scope', '$attrs', 'focusService', function($scope, $attrs, focusService) {
        $scope.placeholder = oppiaHtmlEscaper.escapedJsonToObj($attrs.placeholderWithValue);
        $scope.rows = oppiaHtmlEscaper.escapedJsonToObj($attrs.rowsWithValue);
        $scope.answer = '';
        $scope.labelForFocusTarget = $attrs.labelForFocusTarget || null;

        $scope.schema = {
          type: 'unicode',
          ui_config: {}
        };
        if ($scope.placeholder) {
          $scope.schema.ui_config.placeholder = $scope.placeholder;
        }
        if ($scope.rows && $scope.rows !== 1) {
          $scope.schema.ui_config.rows = $scope.rows;
        }

        $scope.submitAnswer = function(answer) {
//answer variable is equal to the input introduced by voice.
	  if (ans != "none") {
	    answer = ans;
	  }
          if (!answer) {
            return;
          }

          $scope.$parent.$parent.submitAnswer(answer, 'submit');
        };
      }]
    };
  }
]);


oppia.directive('oppiaResponseTextInput', [
  'oppiaHtmlEscaper', function(oppiaHtmlEscaper) {
    return {
      restrict: 'E',
      scope: {},
      templateUrl: 'response/TextInput',
      controller: ['$scope', '$attrs', function($scope, $attrs) {
        $scope.answer = oppiaHtmlEscaper.escapedJsonToObj($attrs.answer);
      }]
    };
  }
]);
